#include "SupperIS.h"
#include "RSManager.h"
#include "Box.h"
#include "BulletManager.h"
#include "Sprite.h"

SupperIS::SupperIS(Box*	box): IS(box)
{
	init();
}


SupperIS::~SupperIS(void)
{
}

void SupperIS::init()
{
	RSManager* ins = RSManager::getInstance()->getInstance();
	s_left = ins->getSprite(IS_SHOOT_LEFT);
	s_right = ins->getSprite(IS_SHOOT_RIGHT);
	l_left = ins->getSprite(IS_LIE_SHOOT_LEFT);
	l_right = ins->getSprite(IS_LIE_SHOOT_RIGHT);
	shoot_delay = 0;
	animation_time = 0;
	IS::init();
	is_state = NORMAL;
}

void SupperIS::update()
{
	
	switch (is_state)
	{
	case NORMAL:
		shoot_delay += GAME_TIME;
		if(shoot_delay > 2000)
		{
			shoot_delay = 0;		
			if(isOnGround())
			{
				animation_time = 0;
				int ai_ramdom =  rand() % 2;
				is_state = SHOOT;
				_box->_v.x = 0.0f;
				if(_direction == 1)
				{			
					shoot_angle = 0.0f;
					if(ai_ramdom == 0)
					{
						_sprite = s_right;
						shoot_pos.x = _box->getRight();
						shoot_pos.y = _box->getBottom() - 20;
						
					}
					else
					{
						_sprite = l_right;
						shoot_pos.x = _box->getRight();
						shoot_pos.y = _box->getTop() + 20;

					}
				}
				else
				{				
					shoot_angle = 3.14f;
					if(ai_ramdom == 0)
					{
						_sprite = s_left;
						shoot_pos.x = _box->getLeft();
						shoot_pos.y = _box->getBottom() - 20;
						
					}
					else
					{
						_sprite = l_left;
						shoot_pos.x = _box->getLeft();
						shoot_pos.y = _box->getTop() + 20;
					}
				}
			}
			_box->_size = _sprite->getRenderSize();
			return;
		}
		IS::update();
		break;
	case SHOOT:
		animation_time += GAME_TIME;
		if(animation_time > 500)
		{
			animation_time = 0;			
			if(_sprite->getCount() == _sprite->GetCurrentIndex() + 1)
			{
				BulletManager::getInstance()->shoot(B_E,_obj,shoot_pos, shoot_angle);
				_sprite = _direction == 1 ? right : left;
				is_state = NORMAL;
				_box->_v.x = IS_MOVE_VEC * _direction;
			}
			else
			{
				_sprite->Next();
			}
		}

		break;
	}

}

void SupperIS::render()
{
	IS::render();
}

Box* SupperIS::getCollisionBox()
{
	_colBox->_position = _box->_position;
	_colBox->_size = _sprite->getRenderSize();
	_colBox->_v = _box->_v;
	_colBox->_v.y = (isOnSpace()) ? _colBox->_v.y : 0;
	return _colBox;
}
